0.1 1 Intro

This is the exploratory analysis for school in session versus break and the impacts on mean road speeds

0.2 2.1 Libraries

library(sf)
## Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rgeoboundaries)
library(osmextract)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright.
## Check the package website, https://docs.ropensci.org/osmextract/, for more details.
library(tmap)

0.3 2.2 Data download & process

#uber <- read_csv("movement-speeds-quarterly-by-hod-nairobi-2018-Q1.csv")
uber_feb <- read_csv("movement-speeds-hourly-nairobi-2019-2.csv")
uber_april <- read_csv("movement-speeds-hourly-nairobi-2019-4.csv")
nairobi_roads <- st_read("nairobi_2019.geojson")
## Reading layer `nairobi_2019' from data source 
##   `/home/mreinmuth/giscience/git/nairobi_uber/nairobi_2019.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 402236 features and 5 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: 35.45218 ymin: -8785269 xmax: 17570570 ymax: 0.1982375
## Projected CRS: WGS 84 / Pseudo-Mercator
st_crs(nairobi_roads) <- 4326
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
holiday_series <- seq(6,27,1)

school_break <- uber_april |>  
  filter(day %in% holiday_series)

school_insession <- uber_feb |>  
  filter(day %in% holiday_series)

school_break_mean <- school_break |>  
  group_by(hour, segment_id, start_junction_id, end_junction_id, 
           osm_way_id, osm_start_node_id, osm_end_node_id) |>  
  summarise(mean_speed_kph = mean(speed_kph_mean, na.rm = TRUE), .groups = "drop")

school_insession_mean <- school_insession |> 
  group_by(hour, segment_id, start_junction_id, end_junction_id, 
           osm_way_id, osm_start_node_id, osm_end_node_id)  |>  
  summarise(mean_speed_kph = mean(speed_kph_mean, na.rm = TRUE), .groups = "drop")
school_insession_mean_6am <- school_insession_mean |> 
  filter(hour == 6)

school_break_mean_6am <- school_break_mean |> 
  filter(hour == 6)

speed_dif_6am <- school_break_mean_6am |> 
  left_join(school_insession_mean_6am, by = c("osm_start_node_id", "osm_end_node_id")) |> 
  mutate(diff_speed_kph = mean_speed_kph.x - mean_speed_kph.y)
school_insession_mean_6am <- school_insession_mean_6am |> 
  left_join(nairobi_roads, by = c("osm_start_node_id" = "osmstartnodeid", "osm_end_node_id" = "osmendnodeid")) |> 
  st_as_sf()

school_break_mean_6am <- school_break_mean_6am |> 
  left_join(nairobi_roads, by = c("osm_start_node_id" = "osmstartnodeid", "osm_end_node_id" = "osmendnodeid")) |> 
  st_as_sf()


speed_dif_6am <- speed_dif_6am |> 
  left_join(nairobi_roads, by = c("osm_start_node_id" = "osmstartnodeid", "osm_end_node_id" = "osmendnodeid")) |> 
  st_as_sf()

0.4 3 ESDA

tmap_mode("view")
## tmap mode set to interactive viewing
tm1 <- tm_shape(school_insession_mean_6am) +
  tm_lines("mean_speed_kph")

tm2 <- tm_shape(school_break_mean_6am) +
  tm_lines("mean_speed_kph")

tm3 <- tm_shape(speed_dif_6am) +
  tm_lines("diff_speed_kph")


tmap_arrange(tm1,tm2,tm3, sync = T)
## Warning: The shape school_insession_mean_6am contains empty units.
## Warning: The shape school_break_mean_6am contains empty units.
## Warning: The shape speed_dif_6am contains empty units.
## Variable(s) "diff_speed_kph" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
## Warning: The shape school_insession_mean_6am contains empty units.
## Warning: The shape school_break_mean_6am contains empty units.
## Warning: The shape speed_dif_6am contains empty units.
## Variable(s) "diff_speed_kph" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
hist(speed_dif_6am$diff_speed_kph)